// pages/bank-card/bank-card.js
const SERVICE = require("../../service.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    getBankCard(this)

    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  bankCard(e){
    let openid = wx.getStorageSync("token");    
    let id = e.currentTarget.dataset.id;
    defaultBankCard(this,{id,openid});
  },
  _CStart(e) {
    this.StartX = e.changedTouches[0].pageX;
  },
  _CMove(e) {
    let Item = e.currentTarget.dataset.index;
    this.MoveX = e.changedTouches[0].pageX;
    if (this.StartX - this.MoveX > 60) {
      this.setData({
        ShowActive: true,
        Item
      });
    } else if (this.MoveX > this.StartX + 60) {
      this.setData({
        ShowActive: false,
        Item
      });
    }
  },
  hidden(e) {
    let _this = this,
    id = e.currentTarget.dataset.id;
    let openid = wx.getStorageSync("token");
      
    wx.showModal({
      title: "提示",
      content: "确定要删除吗？",
      confirmColor: "#31AEFE",
      success: function (res) {
        if (res.confirm) {
          SERVICE.hidden({ bank_card_id:id, openid }, (status, data) => {
            if (status) {
              getBankCard(_this);
              _this.setData({
                ShowActive: false
              });
            }
          });
        } else if (res.cancel) {
          _this.setData({
            ShowActive: false
          });
        }
      }
    });
  }
})

function getBankCard(e){
  let openid = wx.getStorageSync("token");  
  wx.showNavigationBarLoading();
  SERVICE.getBankCard({ openid }, (status, data) => {
    wx.hideNavigationBarLoading();   
    let BankCardList = [];
    for (let i in data.data) {
      BankCardList.push(data.data[i]);
    }
    e.setData({
      BankCardList
    }); 
    if (BankCardList.length > 0 && BankCardList.length < 2) {
      SERVICE.defaultBankCard({
        id: BankCardList[0].bank_card_id,
        openid
      }, (status, data) => {
        console.log(data);
      });
    }
    
  });
}

function defaultBankCard(e,params){
  SERVICE.defaultBankCard(params,(status,data)=>{
    console.log(data);
    if(status){
      getBankCard(e);
    }
  });
}